package linear_list.leetcode.easy;

/**
 * @Description: 删除排序链表中的重复元素
 * @Author Stark
 * 题目链接：https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list
 * @Date 2022/4/6 18:46
 **/
public class Num83_DeleteDuplicates {
    int choice = 1;
    public ListNode deleteDuplicates(ListNode head) {
        if(choice == 1) {
            //非递归法
            if (head == null || head.next == null) return head;
            ListNode dummyHead = new ListNode(0);
            ListNode cur = dummyHead, slow = head, fast = head.next;
            while (fast != null) {
                if (slow.val != fast.val) {
                    cur = cur.next = slow;
                    slow = fast;
                }
                fast = fast.next;
            }
            cur = cur.next = slow;
            cur.next = null;
            return dummyHead.next;
        }else if(choice == 2){
            //递归法
            if(head == null || head.next == null)
                return head;
            if(head.val == head.next.val){
                //值相同时，不拼接，遍历后续的结点
                head = deleteDuplicates(head.next);
            }else{
                //值不相同时,拼接结点
                head.next = deleteDuplicates(head.next);
            }
            //返回拼接后的头结点
            return head;
        }
        return null;
    }
}
